package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.LoginMapper;
import com.itheima.mapper.SubjectMapper;
import com.itheima.mapper.TagsMapper;
import com.itheima.mapper.TokenMapper;
import com.itheima.pojo.BsUser;
import com.itheima.pojo.HmSubjects;
import com.itheima.pojo.HmSubjectsTags;
import com.itheima.service.SubjectService;
import com.itheima.util.PageResult;
import com.itheima.util.SimpSubjectDto;
import com.itheima.util.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private TokenMapper tokenMapper;
    @Autowired
    private TagsMapper tagsMapper;

    @Override
    public void updateById(Integer id, HmSubjects hmSubjects) {
        hmSubjects.setId(id);
        subjectMapper.updateById(hmSubjects);
    }

    @Override
    public void del(Integer id) {
        subjectMapper.deleteById(id);
        LambdaQueryWrapper<HmSubjectsTags> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HmSubjectsTags::getSubjectid,id);
        tagsMapper.delete(wrapper);
    }

    @Override
    public void add(HmSubjects hmSubjects) {
        String authorization = request.getHeader("Authorization");
        String[] s = authorization.split(" ");
        LambdaQueryWrapper<Token> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Token::getToken,s[1]);
        Token token = tokenMapper.selectOne(wrapper1);
        hmSubjects.setCreatorid(token.getBsUserId());
        hmSubjects.setAdddate(new Date());
        hmSubjects.setTags(0);
        hmSubjects.setTotals(0);
        hmSubjects.setTwoleveldirectory(0);
        subjectMapper.insert(hmSubjects);
    }

    @Override
    public List simple(String subjectName) {
        LambdaQueryWrapper<HmSubjects> wrapper = new LambdaQueryWrapper<>();
        if(subjectName!=null)
            wrapper.like(HmSubjects::getSubjectname,subjectName);
        List<HmSubjects> hmSubjects = subjectMapper.selectList(wrapper);
        ArrayList<SimpSubjectDto> list = new ArrayList<>();
        for (HmSubjects hmSubject : hmSubjects) {
            SimpSubjectDto simpSubjectDto = new SimpSubjectDto();
            simpSubjectDto.setValue(hmSubject.getId());
            simpSubjectDto.setLabel(hmSubject.getSubjectname());
            list.add(simpSubjectDto);
        }
        PageResult pageResult = new PageResult();
        pageResult.setItems(list);
        return list;
    }

    @Override
    public HmSubjects getById(Integer id) {
        HmSubjects hmSubjects = subjectMapper.selectById(id);
        BsUser bsUser = loginMapper.selectById(hmSubjects.getCreatorid());
        hmSubjects.setUsername(bsUser.getUsername());
        return hmSubjects;
    }

    @Override
    public PageResult list(Integer page, Integer pagesize, String subjectName) {
        LambdaQueryWrapper<HmSubjects> wrapper = new LambdaQueryWrapper<>();
        if(subjectName!=null)
            wrapper.like(HmSubjects::getSubjectname,subjectName);
        IPage<HmSubjects> p=new Page<>(page,pagesize);
        IPage<HmSubjects> hmSubjectsIPage = subjectMapper.selectPage(p, wrapper);
        for (HmSubjects record : hmSubjectsIPage.getRecords()) {
            BsUser bsUser = loginMapper.selectById(record.getCreatorid());
            record.setUsername(bsUser.getUsername());
        }
        PageResult pageResult = new PageResult();
        pageResult.setCounts(Integer.parseInt(hmSubjectsIPage.getTotal()+""));
        pageResult.setItems(hmSubjectsIPage.getRecords());
        pageResult.setPage(hmSubjectsIPage.getCurrent()+"");
        pageResult.setPages(Integer.parseInt(hmSubjectsIPage.getPages()+""));
        pageResult.setPagesize(hmSubjectsIPage.getSize()+"");
        return pageResult;
    }
}
